package com.highbury5.blog.util.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Tuple;

import javax.annotation.PostConstruct;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

@Component
public class ClusterRedisComponent implements AbstractRedisComponent{

    @Autowired
    JedisCluster jedisCluster;

    /**
     * 向对应的zset中元素score加1,返回最新score
     */
    @Override
    public double zincrby(String key ,String value){
        double result = jedisCluster.zincrby(key,1,value);
        return result;
    }

    @Override
    public Map<String,Double> zrevrange(String key ,int start,int end){
        Set<Tuple> set =  jedisCluster.zrevrangeWithScores(key,start,end);
        Map<String,Double> map = new LinkedHashMap();
        for(Tuple tuple : set){
            map.put(tuple.getElement(),tuple.getScore());
        }
        return map;
    }

    @Override
    public String get(String key){
        return jedisCluster.get(key);
    }


    @Override
    public Long incr(String key){
        return jedisCluster.incr(key);
    }
}
